package com.wh.basic.utils;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @Desc
 * @Author ssl
 * @Date 2024/5/18 21:53
 **/
public class TimeUtil {
    public static List<String> getNowYearMonth() {
        Calendar calendar = Calendar.getInstance(); // 创建Calendar对象并设置时间为当前时间
        int year = calendar.get(Calendar.YEAR); // 获取当前年份

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); // 定义日期格式化器

        ArrayList<String> dates = new ArrayList<>();

        for (int month = 1; month <= 12; month++) { // 遍历每个月
            calendar.set(year, month - 1, 1); // 将calendar设置为指定年份、月份的第一天

            String formattedDate = sdf.format(calendar.getTime()); // 格式化日期字符串

            dates.add(formattedDate); // 添加到结果列表中
        }

        return dates;
    }

    public static List<String> getNowMonthDay() {
        List<String> dates = new ArrayList<>();
        // 创建 Calendar 对象并设置为当前时间
        Calendar calendar = Calendar.getInstance();

        // 将日期设置为本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        int monthLength = calendar.getActualMaximum(Calendar.DATE);

        for (int i = 0; i < monthLength; i++) {
            String dateStr = String.format("%tF", calendar.getTime());
            dates.add(dateStr);
            // 递增日期
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return dates;
    }

    public static List<String> getNowWeekDay() {
        LocalDate currentDate = LocalDate.now();
        LocalDate firstDayOfWeek = currentDate.with(DayOfWeek.MONDAY);
        // 获取本周的最后一天
        LocalDate lastDayOfWeek = firstDayOfWeek.plusDays(6);
        // 获取本周的所有日期
        List<String> datesOfWeek = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (LocalDate date = firstDayOfWeek; !date.isAfter(lastDayOfWeek); date = date.plusDays(1)) {
            String time = date.format(formatter);
            datesOfWeek.add(time);
        }
        return datesOfWeek;
    }
}
